<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Browse_model extends CI_Model {

	function __construct(){
		parent::__construct();

	}

	function getPost($locID=0, $catID=0, $offset ,$limit ){
		$sql="";
//		print "LOC : ".$locID."---CAT : ".$catID;
		//print $offset.'-----'.$limit; die();

		if($locID == 0 && $catID != 0){
			$sql = "SELECT P.* from post P
			INNER JOIN term T on P.id = T.post_id and (T.term='category' or T.term='keyword') and T.value = $catID
			and status='51' group by P.id Limit $offset,$limit";
		}

		if($locID != 0 && $catID == 0){
			$sql = "SELECT P.* from post P
			INNER JOIN term T on P.id = T.post_id and (T.term='city' or T.term='state' or T.term='country') and T.value = $locID
			and status='51' Limit $offset,$limit";
		}

		if($locID != 0 && $catID != 0){
			$sql = "SELECT P.* from post P
			INNER JOIN term T on P.id = T.post_id
			AND (T.term='category' or T.term='keyword') and T.value = $catID

			INNER JOIN term T2 on P.id = T2.post_id
			AND (T2.term='country' or T2.term='region' or T2.term='city') and T2.value = $locID and status='51'
			Limit $offset,$limit";
		}

		if($locID == 0 && $catID == 0){
			$sql = "SELECT P.* from post P
			INNER JOIN term T on P.id = T.post_id
			Limit $offset,$limit";
		}

		//die ($sql);
		$result = $this->db->query($sql)->result();
		if($result){
			return $result;
		}

	}

	
	function countAlllisting($key_id = 0) {
	
		$sql = "Select count(*)as total from term where value = $key_id and (term='category' or term='keyword')";
		$query = $this->db->query($sql);
		$row = $query->row();
		if ($row->total != 0)
			return $row->total;
		else
			return '0';
	}
	
	function getWebconfiguration($fieldName) {
		$sql = "SELECT $fieldName FROM web_settings";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		return $row->$fieldName;
	}
	
	function send_mail($email = NULL, $subject = NULL, $body = NULL) {
	
		$this->email->clear();
		//--Email Config
		$email_config['protocol'] = 'mail';
		$email_config['wordwrap'] = FALSE;
		$email_config['mailtype'] = 'html';
		$email_config['charset'] = 'utf-8';
		$email_config['crlf'] = "\r\n";
		$email_config['newline'] = "\r\n";
		$this->email->initialize($email_config);
	
		$this->email->to($email);
		$this->email->from($this->member_model->getWebconfiguration('default_email'), $this->member_model->getWebconfiguration('web_title'));
		$this->email->subject($subject);
		$this->email->message($body);
		$this->email->send();
	}
	

}